import { round } from 'lodash';

export default function ({
  data = [
    { value: 335, type: '直达' },
    { value: 310, type: '邮件营销' },
    { value: 234, type: '联盟广告' },
    { value: 135, type: '视频广告' },
    { value: 1048, type: '百度' },
    { value: 251, type: '谷歌' },
    { value: 147, type: '必应' },
    { value: 102, type: '其他' },
  ],
} = {}) {
  // 计算总和
  const total = data.reduce((a, b) => a + b.value, 0);
  let num = 0;
  // 计算百分比
  data.forEach((item, i) => {
    const rate = round((item.value / (total || 1)) * 100, 2);
    num += rate;
    // 100减去之前计算出来的百分比
    item.rate = i !== data.length ? rate : 100 - num;
    item.name = `${item.type}\n${item.value}万吨（${item.rate}%）`;
  });
  return {
    tooltip: {
      trigger: 'item',
      formatter: ({ data }) => {
        const { name, rate, value } = data;
        return `${name}${value}万吨（${rate}%）`;
      },
    },
    series: [
      {
        name: '内环',
        type: 'pie',
        radius: ['35%', '65%'],
        label: {
          show: false,
          position: 'inner',
        },
        labelLine: {
          normal: {
            show: false,
          },
        },
        data,
      },
      {
        name: '外环',
        type: 'pie',
        radius: ['70%', '80%'],
        label: {
          color: '#9DA4A6',
          fontSize: 14,
        },
        data,
      },
    ],
  };
}
